package com.tencent._03_xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception{
        // 申城一个消费者对象
        DefaultMQProducer producer = new DefaultMQProducer("producer_xhr_group");
        // 链接MQ
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动
        producer.start();
        // 创建
        Message message = new Message("xhr_topic", "xhr_tags", "我是xhr的消息".getBytes(StandardCharsets.UTF_8));
        // 发送消息
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功：" + sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送失败");
            }
        });
        //
        System.out.println("执行回调方法之前就要打印的这句话证明是一部请求");
    }
}
